(function (G) {
  window["components_Input_Map_Polygon_Polygon"]({
  "component": true,
  "usingComponents": {
  }
},{
template:'#TEMPLATE_components_Input_Map_Polygon_Polygon',

    props: {
      name: {
        type: String,
        default:G.guid()
      },
      index: {
        type: Number
      },
      disabled: {
        type: Boolean,
        default: false
      },
      value: {
        type: String,
        default: '',
        observer: function (newValue, oldValue) {
          this.fliter(newValue);
        }
      },
      attr:{
        type:Object,
        default:{max:10}
      }
    },
    data: function () {
      return {
        value1: [],
        firstValue:'',
        map:'',
        polyEditor:'',
        polygons:{},
        editing:false
      }
    },
    methods: {
      fliter(value){
        if(value == JSON.stringify(this.value1)) return
        //this.setData({ value1: G.string(value).parse([]) });
        if(this.map) {
          this.map.clearMap()
          this.polyEditor.close()
          this.polygons = {}
          let value1 = G.string(value).parse([])
          for(let i = 0; i < value1.length; i ++) this.addPolygon(value1[i])
        }
      },
      save(){
          let paths = []
          for(let key in this.polygons){
              let path = this.polygons[key].getPath()
              for(let i = 0; i < path.length; i ++) path[i] = [path[i].lng,path[i].lat]
              paths.push(path)
          }
          this.value1 = paths
          this.triggerEvent('change', { value:paths.length ? JSON.stringify(paths) : '',type:'polygon', id: this.getData('name'), index: this.getData('index') })
          this.editing = false
          this.polyEditor.close()
        },
      clear(){
          this.map.clearMap()
          this.polyEditor.close()
          this.polygons = {}
          if(this.firstValue){
            this.fliter(this.firstValue)
          }
      },
      editPolygon(polygon){
          this.polyEditor.close()
          this.polyEditor.addAdsorbPolygons(polygon)
          this.polyEditor.setTarget(polygon)
          this.polyEditor.open()
          this.editing = true
      },
      addPolygon(path){
        return new Promise(next => {
          let polygonConfig = this.getData('attr').polygon || {}
          if(typeof polygonConfig == 'function'){
            polygonConfig(point,this.getData('name')).then(_polygonConfig => {
              next(_polygonConfig)
            })
          }else{
            next(polygonConfig)
          }
        }).then(polygonConfig => {
          return new Promise(next => {
              var polygon = new AMap.Polygon({
                  path,
                  strokeColor: "#FF33FF",
                  strokeWeight: 3,
                  strokeOpacity: 0.2,
                  fillOpacity: 0.4,
                  fillColor: '#1791fc',
                  zIndex: 50,
                  bubble: true,
                  ...polygonConfig
              })
              this.map.add([polygon])
              this.map.setFitView()
              if(!this.getData('disabled')) this.editPolygon(polygon)
              polygon.on('click',event => {
                  if(!this.getData('disabled')) this.editPolygon(event.target)
              })
              polygon.on('dblclick',event => {
                  if(!this.getData('disabled')) {
                  G.confirm('删除标记区域?').then(()=>{
                      this.map.remove(this.polygons[event.target._amap_id])
                      this.polyEditor.close()
                      delete this.polygons[event.target._amap_id]
                  })
                  }else{
                      let path = this.polygons[event.target._amap_id].getPath()
                      for(let i = 0; i < path.length; i ++) path[i] = [path[i].lng,path[i].lat]
                      this.triggerEvent('click', { event, path, type:'polygon', id: this.getData('name'), index: this.getData('index') })
                  }
              })
              this.polygons[polygon._amap_id] = polygon
              next()
          })
        })
      }
    },
    mounted: function () {
      let attr = this.getData('attr') || {}
      attr.max = attr.max || 1
      this.firstValue = this.value
      let point = attr.center || [106.51107, 29.50197]
      this.map = new AMap.Map(this.getData('name'), {
        center:point,
        zoom: attr.zoom || 11
      })
      var autoOptions = {
				input: "keyword"
			};
			var auto = new AMap.AutoComplete(autoOptions);
			var placeSearch = new AMap.PlaceSearch({
				map: this.map
			}) //构造地点查询类
			AMap.Event.addListener(auto, "select", e => {
        placeSearch.setCity(e.poi.adcode);
				placeSearch.search(e.poi.name); //关键字查询查询
				var info = e.poi;//地理信息
      })
      AMap.Event.addListener(auto, "error", e => {
        console.log(e)
      })
      this.polyEditor = new AMap.PolygonEditor(this.map)
      if(this.value){
        this.fliter(this.value)
      }
      // this.map.on('click',event => {
      //   this.polyEditor.close()
      // })
      this.map.on('rightclick', event => {
        let point = [event.lnglat.lng,event.lnglat.lat]
        let path = [
              point,
              [point[0] + 0.0001,point[1] + 0.0001],
              [point[0] + 0.0001,point[1] + 0.0002]
          ]
        if(!this.getData('disabled')) {
          if(this.value1.length < attr.max){
              G.confirm('在此区域绘图?').then(()=>{
                  this.addPolygon(path).then(() => {
                      this.triggerEvent('click', { value: this.value1.length ? JSON.stringify(this.value1) : '',detail:{type:'polygonAdd',path}, id: this.getData('name'), index: this.getData('index') })
                  })
              })
          }
        }
      })
    }
  })
})(Y)
